package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.TimeDataBean;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForPreference;

/**
 * Created by 廉冲-android开发 on 2017/5/17.
 */
public class Time_dataAdapter extends BaseAdapter {
    List<TimeDataBean.DayListBean> dayListBeanList = new ArrayList<>();
    LayoutInflater inflater;
    Context context;

    public Time_dataAdapter() {
    }

    public Time_dataAdapter(List<TimeDataBean.DayListBean> dayListBeanList, Context context) {
        this.dayListBeanList = dayListBeanList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<TimeDataBean.DayListBean> dayListBeanList) {
        this.dayListBeanList = dayListBeanList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return dayListBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return dayListBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_time_data, null);
            viewHolder = new ViewHolder();

            viewHolder.textView_time = (TextView) convertView.findViewById(R.id.textView_time);
            viewHolder.textView_num = (TextView) convertView.findViewById(R.id.textView_num);
            viewHolder.linearLayout_timedate = (LinearLayout) convertView.findViewById(R.id.linearLayout_timedate);
            viewHolder.imageView_date_time = (ImageView) convertView.findViewById(R.id.imageView_date_time);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        TimeDataBean.DayListBean dayListBean = dayListBeanList.get(position);

        String newStr = dayListBean.getDatetimes().substring(6, dayListBean.getDatetimes().length() - 2);
        Long datatime = Long.valueOf(newStr);

        viewHolder.textView_time.setText(getFormatedDateTime("dd",datatime));
        viewHolder.textView_num.setText(dayListBean.getSum()+"");
        getNumColor(viewHolder.textView_num , dayListBean.getSum());//设置文字颜色
        if ((position+1)%7 == 0){
            if (position+1 == 49){
                viewHolder.imageView_date_time.setBackgroundResource(R.drawable.img_xiayou);
            }else {
                viewHolder.imageView_date_time.setBackgroundResource(R.drawable.img_zhongyou);
            }

            if(getFormatedDateTime("yyyy-MM-dd",datatime).equals(ForPreference.getString(ForContents.TIME_DATE_DATETIME))){
                if (position == 48){
                    viewHolder.imageView_date_time.setBackgroundResource(R.drawable.img_youxia);
                }else {
                    viewHolder.imageView_date_time.setBackgroundResource(R.drawable.img_hongyou);
                }
                viewHolder.textView_time.setTextColor(Color.parseColor("#ffffff"));
                viewHolder.textView_num.setTextColor(Color.parseColor("#ffffff"));
                viewHolder.textView_num.setText("今日");
            }

        }else {
            if(getFormatedDateTime("yyyy-MM-dd",datatime).equals(ForPreference.getString(ForContents.TIME_DATE_DATETIME))){
                viewHolder.imageView_date_time.setBackgroundResource(R.drawable.img_hongsekuai);
                viewHolder.textView_time.setTextColor(Color.parseColor("#ffffff"));
                viewHolder.textView_num.setTextColor(Color.parseColor("#ffffff"));
                viewHolder.textView_num.setText("今日");
            }
        }

        return convertView;
    }

    public class ViewHolder {
        TextView textView_time;
        TextView textView_num;
        ImageView imageView_date_time;
        LinearLayout linearLayout_timedate;
    }


    public static String getFormatedDateTime(String dateFormat, long dateTime) {
        SimpleDateFormat sDateFormat = new SimpleDateFormat(dateFormat);
        return sDateFormat.format(new Date(dateTime + 0));
    }

    public void getNumColor(TextView textView , int num){
        int color = 0;
        if (num == 0){
            color = R.color.color_datetime_0;
        }else if(1 <= num && num <= 59){
            color = R.color.color_datetime_1$59;
        }else if(60 <= num && num <= 80){
            color = R.color.color_datetime_60$80;
        }else if(81 <= num && num <= 100){
            color = R.color.color_datetime_81$100;
        }
        textView.setTextColor(context.getResources().getColor(color));
    }

}
